<?php
class Node extends Module {

    /*private $type_id = 0;
     protected $typename = '';

     private $caption;
     private $keywords;
     private $description;
     private $url_segment;
     private $access_mode;*/

    //    public function init() {
    //        //
    //        //out()->add_prejs(tos_cdn_url('js/z.js', 'sys'));
    //    }


    private $content_module;

    /*public function init() {
     print_r($this);
     if ( ! isset($this->typename)) {
     tos_die('node type must be defined');
     }

     if ($type_id = $this->model('node')->get_type_id($this->typename)) {
     $this->type_id = $type_id;
     } else {
     tos_die('not validate node type');
     }
     }*/

    public function display () {
        $node_id = tos_http_get('node_id');
        if (empty($node_id)) {
            tos_die('error request');
        }

        if ($node_data = $this->model('node')->entry($node_id)) {
            $type = $node_data['type'];
            $content_module = m($type);
            if ( ! $content_module) {
                tos_die('error request');
            }
            $node_view = $this->view('node_display', $node_data);
            $node_view->add_data('type', $type);
            if ($content_view = $content_module->display_content($node_id)) {
                $node_view->add_view($content_view);
            }
            return $node_view;
        } else {
            return new View_text('error request');
        }
    }



    public function add() {
        $type = tos_http_get('type');
        if ( ! empty($type)) {
            $content_module = m($type);
            $this->content_module = $content_module;
            if ( ! $content_module) {
                tos_die("$type does not exists!");
            }

            if ($_SERVER['REQUEST_METHOD'] === 'POST') {
                if ($node_id = $this->_add_node_data($type)) {
                    if ($content_id = $content_module->add_content_data($node_id)) {
                        $submit = tos_http_post('submit');
                        $this->redirect($type, $node_id, $submit);
                    }
                }
            }

            $node_view = $this->view('node_edit');
            $content_view = $content_module->display_content_add();
            $node_view->add_data('type', $type);
            $node_view->add_view($content_view);
        } else {
            $node_view = new View_text('No type');
        }

        return $node_view;
    }


    private function _add_node_data($type) {
        $type = (trim($type));
        if (empty($type)){
            return false;
        }


        $caption = tos_http_post('caption');
        $keywords = tos_http_post('keywords');
        $description = tos_http_post('description');
        $url_segment = tos_http_post('url_segment');
        $is_public = tos_http_post('is_public');
        $node_thumbnail = tos_http_post('node_thumbnail');

        if ('on' === $is_public) {
            $access_mode = NODE_ACCESS_PUBLIC;
        } else {
            $access_mode = NODE_ACCESS_PRIVATE;
        }

        $description = trim($description);
        if (empty($description)) {
            $description = $this->content_module->get_description();
        }

        return $this->add_node($type, $caption, $keywords, $description, $url_segment, $access_mode, $node_thumbnail);
    }
    

    public function edit() {

        $node_id = tos_http_get('node_id');
        if (empty($node_id)) {
            tos_die('error request!');
        }
        $node_data = $this->model('node')->entry($node_id);
        if ( ! $node_data) {
            tos_die('cannot find node by node_id ' . $node_id);
        }
        $type = $node_data['type'];

        $content_module = m($type);
        $this->content_module = $content_module;
        if ( ! $content_module) {
            tos_die("$type does not exists!");
        }

        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            if ($this->edit_node_data($node_id)) {
                if ($content_module->edit_content_data($node_id)) {
                    $submit = tos_http_post('submit');
                    $this->redirect($type, $node_id, $submit);
                }
            }
        }

        //$content_data = $content_module->get_content_data($node_id);
        $node_view = $this->view('node_edit', $node_data);
        $content_view = $content_module->display_content_edit($node_id);
        //$node_view->add_data('title', $content_module->get_title());
        $node_view->add_view($content_view);

        return $node_view;
    }


    
    public function display_search() {
        return $this->view('node_search');
    }


    public function add_node($type = '', $caption = '', $keywords = '',
    $description = '', $url_segment = '', $access_mode = '', $thumbnail = '') {

        $caption = trim($caption);
        if (empty($caption)) {
            return false;
        }

        $type = trim($type);
        if (empty($type)) {
            return false;
        }

        if ($access_mode != NODE_ACCESS_PUBLIC) {
            $access_mode = NODE_ACCESS_PRIVATE;
        }

        $url_segment = trim($url_segment);
        if (empty($url_segment)) {
            $url_segment = $caption;
        }
        $url_segment = tos_clean_url_segment($url_segment);

        $user_id = m('user')->get_user_id();
        if ( ! $user_id) {
            return false;
        }
        $description = trim($description);

        /*$has_thumbnail = 0;
         $thumbnail = trim($thumbnail);
         if ( ! empty($thumbnail)) {
         $has_thumbnail = 1;
         }*/

        $node_id = $this->model('node')->add(
        $user_id, $type, $caption, $keywords, $description, $url_segment, $access_mode, $thumbnail
        );

        $key_arr1 = tos_explode_keywords($caption);
        $key_arr2 = tos_explode_keywords($caption, ' ');
        $key_arr3 = tos_explode_keywords($keywords);

        $key_arr = array_merge($key_arr1, $key_arr2, $key_arr3);
        $key_arr = array_unique($key_arr);

        $this->model('mapping')->add_keywords($key_arr, $node_id);

        //$thumbnail = trim($thumbnail);
        /*if ($has_thumbnail) {
        $this->model('node_image')->add($node_id, 'thumbnail', $thumbnail, $caption);
        }*/

        return $node_id;
    }


    /*public function add_node_img($node_id, $key, $src, $alt = '') {

    }*/

    public function display_list() {
        $type = tos_http_get('type');
        $page_index = tos_http_get('page');
        $parent_id = tos_http_get('parent_id');

        $page_index = (int) $page_index;

        $length = 20;
        $offset = $page_index * $length;

        $list = $this->model('node')->entries($offset, $length, $parent_id, $type);
        $data['list'] = $list;
        return $this->view('node_list', $data);
    }


    private function edit_node_data($node_id) {
        $caption = tos_http_post('caption');
        $keywords = tos_http_post('keywords');
        $description = tos_http_post('description');
        $url_segment = tos_http_post('url_segment');
        $is_public = tos_http_post('is_public');
        $node_thumbnail = tos_http_post('node_thumbnail');

        $access_mode = ('on' === $is_public) ? NODE_ACCESS_PUBLIC : '';

        return $this->model('node')->update(
        $node_id, $caption, $keywords, $description, $url_segment, $access_mode, $node_thumbnail);
    }





    /*public function get_title() {
     return 'Add Node';
     }

     protected function add_content_view() {

     }*/


    private function redirect($type, $node_id, $submit) {
        if ($submit == 'save') {
            tos_redirect(array(
                'm'         => 'node',
                'a'         => 'edit',
                'type'      => $type,
                'node_id'   => $node_id
            ));
        } else {
            tos_redirect(array(
                'm'         => 'node',
                'a'         => 'display',
                'type'      => $type,
                'node_id'   => $node_id
            ));
        }
    }


    /*private function get_type_id() {
     if ( ! $this->type_id) {
     if ($type_id = $this->model('node')->get_type_id($this->typename)) {
     $this->type_id = $type_id;
     } else {
     return false;
     }
     }
     return $this->type_id;
     }*/


}
?>